$(function(){
	//减少按钮触发
	$(".reduce").on("click",function(){
		var shoppingCartItemId = $(this).attr("name");
		var shoppingCartItemAmount = parseInt($("#"+shoppingCartItemId).val());
			var startingBatch = parseInt($("#"+shoppingCartItemId).attr("data-startingBatch"));
			if(shoppingCartItemAmount>startingBatch){
				shoppingCartItemAmount = shoppingCartItemAmount-1;
				$("#"+shoppingCartItemId).attr("value",shoppingCartItemAmount);
				$("#"+shoppingCartItemId).val(shoppingCartItemAmount);
				var amount = $("#"+shoppingCartItemId).val();
				var oldAmount = $("#"+shoppingCartItemId).attr("data-amount");
				var changeAmount = amount-oldAmount;
				var shoppingCartId = $("#"+shoppingCartItemId).attr("data-shoppingCartId");
				var price = $(this).parents(".info").find(".price").text();
				$("#"+shoppingCartItemId).attr("data-amount",amount);
				if($(this).parents(".product").find(".select").hasClass("iconfont")){
					changeAmounts(shoppingCartItemId,amount,shoppingCartId,changeAmount,price,'1');
				}else{
					changeAmounts(shoppingCartItemId,amount,shoppingCartId,changeAmount,price,'0');
				}
	
			}  
	});
	//增加按钮触发
	$(".add").on("click",function(){
		var shoppingCartItemId = $(this).attr("name");
		var shoppingCartItemAmount = parseInt($("#"+shoppingCartItemId).val());
		shoppingCartItemAmount = shoppingCartItemAmount+1;
		$("#"+shoppingCartItemId).attr("value",shoppingCartItemAmount);
		$("#"+shoppingCartItemId).val(shoppingCartItemAmount);
		var amount = $("#"+shoppingCartItemId).val();
		var oldAmount = $("#"+shoppingCartItemId).attr("data-amount");
		var changeAmount = amount-oldAmount;
		var shoppingCartId = $("#"+shoppingCartItemId).attr("data-shoppingCartId");
		var price = $(this).parents(".info").find(".price").text();
		$("#"+shoppingCartItemId).attr("data-amount",amount);
		if($(this).parents(".product").find(".select").hasClass("iconfont")){
			changeAmounts(shoppingCartItemId,amount,shoppingCartId,changeAmount,price,'1');
		}else{
			changeAmounts(shoppingCartItemId,amount,shoppingCartId,changeAmount,price,'0');
		}
		
	});
	
	//文本输入框改变购买数量
	$(".num_txt").change(function(){
		var amount = $(this).val().replace(/\s+/g,"");
		var oldAmount = $(this).attr("data-amount");
		var startingBatch = $(this).attr("data-startingBatch");
		var shoppingCartItemId = $(this).attr("id");
		if(amount!=""){
			if(isNaN(amount)){
				tip.content("购买数量必须为阿拉伯数字").show();
					$(this).val(oldAmount);
					$(this).attr("value",oldAmount);
			}else{
					if(parseInt(amount)<parseInt(startingBatch)){
						tip.content("选择的购买数量不能小于起批量").show();
						$(this).attr("value",startingBatch);
						$(this).val(startingBatch);
						var amount = $(this).val();
						var oldAmount = $(this).attr("data-amount");
						var changeAmount = amount-oldAmount;
						var shoppingCartId = $(this).attr("data-shoppingCartId");
						var price = $(this).parents(".info").find(".price").text();
						$(this).attr("data-amount",qingxiaoStartingBatch);
						if($(this).parents(".product").find(".select").hasClass("iconfont")){
							changeAmounts(shoppingCartItemId,amount,shoppingCartId,changeAmount,price,'1');
						}else{
							changeAmounts(shoppingCartItemId,amount,shoppingCartId,changeAmount,price,'0');
						}
					}else{
						var amount = $(this).val();
						var oldAmount = $(this).attr("data-amount");
						var changeAmount = amount-oldAmount;
						var shoppingCartId = $(this).attr("data-shoppingCartId");
						var price = $(this).parents(".info").find(".price").text();
						$(this).attr("data-amount",amount);
						if($(this).parents(".product").find(".select").hasClass("iconfont")){
							changeAmounts(shoppingCartItemId,amount,shoppingCartId,changeAmount,price,'1');
						}else{
							changeAmounts(shoppingCartItemId,amount,shoppingCartId,changeAmount,price,'0');
						}
					}
			}
		}else{
			tip.content("购买数量不能为空").show();
				$(this).val(oldAmount);
				$(this).attr("value",oldAmount);			
		}
		});
	//修改购买数量
	function changeAmounts(shoppingCartItemId,amount,shoppingCartId,changeAmount,price,isAdd){
		var id = shoppingCartItemId.substring(16);
		var changeCount = parseInt(changeAmount)*parseFloat(price);
		$.ajax({
			type:"get",
			url:base_url+"customer/changeSCIAmount",
			data:{"shoppingCartItemId":id,"amount":amount},
			async:false,
			success:function(data){
				var result=JSON.parse(data).data;
				if(result=='true'){
					if(isAdd=='1'){
						changeShopCartMsgOne(shoppingCartId,changeCount,changeAmount);
					}
				}else{
					tip.content("购物车项不存在").show();
					window.location.href =base_url+"customer/getShoppingCarts";
				}
			},
			error:function(){
			}
		});
	}
	//点击商店选择按钮
	$(".shop_title .select_targe").on("click",".select",function(){
		var _this = $(this);
		var shoppingCartItemIds = new Array();
		var shoppingCartId =  $(this).attr("data-shoppingCartId");
		var changeAmountAll = parseInt('0');
		var changeCountAll = parseFloat('0.0');
		//判断商品
		if(_this.hasClass("iconfont")){
				//获得所有购物车项
		_this.parents(".shop").find(".product").each(function(){
				
				shoppingCartItemIds.push($(this).find(".select").attr("data-shoppingCartItemId"));
				if($(this).find(".select").hasClass("iconfont")){
					var shoppingCartItemId = $(this).find(".select").attr("data-shoppingCartItemId");
					var shoppingCartItemAmount = parseInt(0)-parseInt($("#shoppingCartItem"+shoppingCartItemId).val());
					var price = $("#shoppingCartItem"+shoppingCartItemId).parents(".info").find(".price").text();
					var changeCount = parseFloat(price)*shoppingCartItemAmount;
					changeCountAll = changeCountAll + changeCount;
					changeAmountAll = changeAmountAll + shoppingCartItemAmount;
				}
				
		});
		var jsonStr = JSON.stringify(shoppingCartItemIds);
			$.ajax({
				type:"get",
				url:base_url+"customer/changeAllSelected",
				data:{"jsonStr":jsonStr,"status":"2"},
				async:false,
				success:function(data){
					var result=JSON.parse(data).data;
					if(result=='true'){
						_this.toggleClass("iconfont").toggleClass("icon-icon");
						_this.parents(".shop").find(".product").each(function(){
							$(this).find(".select").removeClass("iconfont").removeClass("icon-icon");
						});
						changeShopCartMsgOne(shoppingCartId,changeCountAll,changeAmountAll);
					}else{
						tip.content("购物车项不存在").show();
						window.location.href= base_url+"customer/getShoppingCarts";
					}
				},
				error:function(){
				
				}
				
			});	
			
		}else{
			
			_this.parents(".shop").find(".product").each(function(){
				
				shoppingCartItemIds.push($(this).find(".select").attr("data-shoppingCartItemId"));
				if(!$(this).find(".select").hasClass("iconfont")){
					var shoppingCartItemId = $(this).find(".select").attr("data-shoppingCartItemId");
					var shoppingCartItemAmount = parseInt(0)+parseInt($("#shoppingCartItem"+shoppingCartItemId).val());
					var price = $("#shoppingCartItem"+shoppingCartItemId).parents(".info").find(".price").text();
					var changeCount = parseFloat('0.0')+parseFloat(price)*shoppingCartItemAmount;
					changeCountAll = changeCountAll + changeCount;
					changeAmountAll = changeAmountAll + shoppingCartItemAmount;
				}
			
			});
			var jsonStr = JSON.stringify(shoppingCartItemIds);
			$.ajax({
				type:"get",
				url:base_url+"customer/changeAllSelected",
				data:{"jsonStr":jsonStr,"status":"1"},
				async:false,
				success:function(data){
					var result=JSON.parse(data).data;
					if(result=='true'){
						_this.toggleClass("iconfont").toggleClass("icon-icon");
						_this.parents(".shop").find(".product").each(function(){
							$(this).find(".select").addClass("iconfont").addClass("icon-icon");
						});
						changeShopCartMsgOne(shoppingCartId,changeCountAll,changeAmountAll);
					}else{
						tip.content("购物车项不存在").show();
						window.location.href =base_url+"customer/getShoppingCarts";
					}
				},
				error:function(){
				}
				
			});	
		}
	});
	//点击商品选择按钮
	$(".product .select_targe").on("click",".select",function(){
		var _this = $(this),
			 shop = _this.parents(".shop"),
			//店家
		     shop_select = shop.find(".shop_title").find(".select"),
			flag = true;
		var shoppingCartItemId = $(this).attr("data-shoppingCartItemId");
		//取消选中
		if(_this.hasClass("iconfont")){
			$.ajax({
				type:"get",
				url:base_url+"customer/changeSelected",
				data:{"shoppingCartItemId":shoppingCartItemId,"status":'2'},
				async:false,
				success:function(data){
					var result=JSON.parse(data).data;
					if(result=='true'){
						_this.toggleClass("iconfont").toggleClass("icon-icon");
						if(shop_select.hasClass("iconfont")){
							shop_select.toggleClass("iconfont").toggleClass("icon-icon");
						
						}
						var shoppingCartId = $("#shoppingCartItem"+shoppingCartItemId).attr("data-shoppingCartId");
						var changeAmount = parseInt(0)-$("#shoppingCartItem"+shoppingCartItemId).val();
						var price = parseFloat($("#shoppingCartItem"+shoppingCartItemId).parents(".info").find(".price").text());
						var changeCount = parseInt(changeAmount)*parseFloat(price);
						changeShopCartMsgOne(shoppingCartId,changeCount,changeAmount);
					}else{
						tip.content("购物车项不存在").show();
						window.location.href=base_url+"customer/getShoppingCarts";
					}
				},
				error:function(){
				}
			});
		}else{
			$.ajax({
				type:"get",
				url:base_url+"customer/changeSelected",
				data:{"shoppingCartItemId":shoppingCartItemId,"status":'1'},
				async:false,
				success:function(data){
					var result=JSON.parse(data).data;
					if(result=='true'){
						_this.toggleClass("iconfont").toggleClass("icon-icon");
						shop.find(".product").each(function(){
							if(!$(this).find(".select").hasClass("iconfont")){
								flag = false;
							}
						})
						if(flag){
							shop_select.toggleClass("iconfont").toggleClass("icon-icon");
						}
						var shoppingCartId = $("#shoppingCartItem"+shoppingCartItemId).attr("data-shoppingCartId");
						var changeAmount = parseInt($("#shoppingCartItem"+shoppingCartItemId).val());
						var price = parseFloat($("#shoppingCartItem"+shoppingCartItemId).parents(".info").find(".price").text());
						var changeCount = parseInt(changeAmount)*parseFloat(price);
						changeShopCartMsgOne(shoppingCartId,changeCount,changeAmount);
					}else{
						tip.content("购物车项不存在").show();
						window.location.href =base_url+"customer/getShoppingCarts";
					}
				},
				error:function(){
				}
			});
		}
		
	});
	//点击全选
	$(".cart_bottom .select_targe").on("click",".select",function(){
		$(this).toggleClass("iconfont").toggleClass("icon-icon");
		var cart = $(this).parents(".cart").find(".shop_list");
		var productItemIds = new Array();
		if($(this).hasClass("iconfont")){
			cart.find(".shop").each(function(){
				
				$(this).find(".select").addClass("iconfont").addClass("icon-icon");
			})
		}else{
			cart.find(".shop").each(function(){
				$(this).find(".select").removeClass("iconfont").removeClass("icon-icon");
			})
		}
	})
	//修改每一个购物车的总信息，当只有一个购物车项发生修改时
	function changeShopCartMsgOne(shoppingCartId,changeCount,changeAmount){
		var oldCount = parseFloat($("#shoppingCart"+shoppingCartId).find('span[name="allCount"]').text());
		var oldNumber = parseInt($("#shoppingCart"+shoppingCartId).find('span[name="allNumber"]').text());
		var newNumber = oldNumber + parseInt(changeAmount);
		var newCount = oldCount+changeCount;
		var oldAllShoppingCartCount = $("#allShoppingCartCount").text();
		var oldAllShoppingCartNumber = $("#allShoppingCartNumber").text();
		var newAllShoppingCartCount = parseFloat(oldAllShoppingCartCount) + parseFloat(changeCount);
		var newAllShoppingCartNumber = parseInt(oldAllShoppingCartNumber) + parseInt(changeAmount);
		$("#shoppingCart"+shoppingCartId).find('span[name="allCount"]').text(parseFloat(newCount).toFixed(2));
		$("#shoppingCart"+shoppingCartId).find('span[name="allNumber"]').text(newNumber);
		$("#allShoppingCartNumber").text(newAllShoppingCartNumber);
		$("#allShoppingCartCount").text(newAllShoppingCartCount.toFixed(2));
	}
	//删除购物车项
	$(".delete").on("click",function(){
		var shoppingCartItemId = $(this).attr("data-shoppingCartItemId");
		var url = base_url + "customer/deleteShoppingCartItem";
		post(url,{"shoppingCartItemId":shoppingCartItemId});
	});
	//去结算
	$("#goBuyIt").on("click",function(){
		var allCount = $(this).parents("div").find("#allShoppingCartCount").text();
		if(parseFloat(allCount)>parseFloat('0.0')){
			window.location.href = base_url + "customer/getBuyNormalProductMsg";
		}else{
			tip.content("至少选择一个购物车项！").show();
		}
	});
	
	//查看运费
	$('button[name="seeFreight"]').click(function(){
		
		var sellerId = $(this).attr("data-sellerId");
		$(".check-postage").find("tbody").empty();
		$.ajax({
			type:"get",
			url:base_url+"customer/getShopPostageMsgs",
			data:{"sellerId":sellerId},
			async:false,
			success:function(data){
				console.log(data);
				var othersArea = '<tr>'
					+'<td>其他</td>'
					+'<td>'+data.deProductFreight+'元</td>'
					+'<td>'+data.deFulfilWithoutFreight+'元</td>'
					+'</tr>';
				$(".check-postage").find("tbody").append(othersArea);
				var postageMsgs = data.postageMsgs;
				for(var i=0;i<postageMsgs.length;i++){
					var area = '<tr>'
						+'<td>'+postageMsgs[i].provinceName+'</td>'
						+'<td>'+postageMsgs[i].postageSystem.productFreight+'元</td>'
						+'<td>'+postageMsgs[i].postageSystem.fulfilWithoutFreight+'元</td>'
						+'</tr>'
						$(".check-postage").find("tbody").append(area);
				}
			},
			error:function(){
			}
		});
		$(".check-postage").fadeIn();
	});
	
});

window.onscroll = function(){
	lazyLoad($(".lazy[data-load='false']"));
}
